/**
 * 功能：设置cookie，永久，不过期, 参数为cookie-key, value
 */
function setCookie(name, value) {
    document.cookie = name + '=' + value + ';expires=' + 'Fri, 31 Dec 9999 23:59:59 GMT'
}

/**
 * 功能：从cookie中取值，参数为cookie-key
 */
function getCookie(name) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(name + '=')
        if (start !== -1) {
            start = start + name.length + 1
            let end = document.cookie.indexOf(';', start)
            if (end === -1) end = document.cookie.length
            return unescape(document.cookie.substring(start, end))
        }
    }
    return 'not found' + name;
}

/**
 * 功能：获取当前时间
 */
function now() {
    return new Date();
}

/**
 * 功能：获取当前时间，单位是ms
 */
function nowMS() {
    return new Date().getTime();
}

/**
 * 功能：发起http GET请求，请求参数为url，返回值为json
 */
function get(url) {
    var http = new XMLHttpRequest();
    console.log("GET请求地址:", url);
    http.open("GET", url, false);
    http.send();
    var res = http.responseText;
    try {
        return JSON.parse(res);
    } catch (e) {
        return res;
    }
}

/**
 * 功能：发起http POST请求，请求参数为url，json数据，返回值为json
 */
function post(url, data) {
    var http = new XMLHttpRequest();
    console.log("POST请求地址:", url, "请求参数:", data);
    http.open("POST", post, false);
    http.setRequestHeader('content-type', 'application/json');
    http.send(JSON.stringify(data));
    var res = http.responseText;
    try {
        return JSON.parse(res);
    } catch (e) {
        return res;
    }
}

/**
 * 功能：在当前页面添加js文件，参数为js文件源地址
 */
function importJs(jsPath) {
    //引入js
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.src = jsPath;
    script.type = 'text/javascript';
    head.appendChild(script);
}

/**
 * 功能：在当前页面添加css文件，参数为css文件源地址
 */
function importCss(cssPath) {
    //引入css
    var head = document.getElementsByTagName('head')[0];
    var link = document.createElement('link');
    link.href = cssPath;
    link.rel = 'stylesheet';
    link.type = 'text/css';
    head.appendChild(link);
}

/**
 * 功能：从当前页面移除css文件，参数为移除引入的文件名
 */
function removeCss(fileName) {
    try {
        let targetelement = "link";
        let targetattr = "href";
        let allsuspects = document.getElementsByTagName(targetelement)
        for (var i = allsuspects.length; i >= 0; i--) {
            if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1) {
                allsuspects[i].parentNode.removeChild(allsuspects[i])
            }
        }
    } catch (e) { }
}

/**
 * 功能：睡眠，参数为等待时间，单位为ms
 */
function sleep(time) {
    var timeStamp = new Date().getTime();
    var endTime = timeStamp + time;
    while (true) {
        if (new Date().getTime() > endTime) {
            return;
        }
    }
}

/**
 * 功能：判断字符串非空
 */
function isNotBlank(value) {
    if (value !== undefined && value !== null && value !== ''
        && !isNaN(value)) {
        return true;
    }
    return false;
}

/**
 * 功能：设置页面全屏
 */
function enterFullScreen() {
    let elem = document.body;
    elem.webkitRequestFullScreen
        ? elem.webkitRequestFullScreen()
        : elem.mozRequestFullScreen
            ? elem.mozRequestFullScreen()
            : elem.msRequestFullscreen
                ? elem.msRequestFullscreen()
                : elem.requestFullScreen
                    ? elem.requestFullScreen()
                    : alert("浏览器不支持全屏");
}

/**
 * 功能：退出页面全屏
 */
function exitFullscreen() {
    let elem = parent.document;
    elem.webkitCancelFullScreen
        ? elem.webkitCancelFullScreen()
        : elem.mozCancelFullScreen
            ? elem.mozCancelFullScreen()
            : elem.cancelFullScreen
                ? elem.cancelFullScreen()
                : elem.msExitFullscreen
                    ? elem.msExitFullscreen()
                    : elem.exitFullscreen
                        ? elem.exitFullscreen()
                        : alert("切换失败,可尝试Esc退出");
}

/**
 * 功能：求取数组中非NaN数据中的最大值
 * max([1, 2, '11', null, 'fdf', []]) ==> 11
 */
function max(list) {
    list = list.filter(item => isNotBlank(item));
    return list.length ? Math.max.apply(null, list) : undefined
}

/**
 * 功能：求取数组中非NaN数据中的最小值
 * min([1, 2, '11', null, 'fdf', []]) ==> 1
 */
function min(list) {
    list = list.filter(item => isNotBlank(item));
    return list.length ? Math.min.apply(null, list) : undefined
}

/**
 * 功能：判断是否是pc端页面
 */
function isPCBroswer() {
    let e = navigator.userAgent.toLowerCase()
        , t = "ipad" == e.match(/ipad/i)
        , i = "iphone" == e.match(/iphone/i)
        , r = "midp" == e.match(/midp/i)
        , n = "rv:1.2.3.4" == e.match(/rv:1.2.3.4/i)
        , a = "ucweb" == e.match(/ucweb/i)
        , o = "android" == e.match(/android/i)
        , s = "windows ce" == e.match(/windows ce/i)
        , l = "windows mobile" == e.match(/windows mobile/i);
    return !(t || i || r || n || a || o || s || l)
}

/**
 * 功能：返回一个随机数
 * random(0, 0.5) ==> 0.3567039135734613
 * random(2, 1) ===> 1.6718418553475423
 * random(-2, -1) ==> -1.4474325452361945
 */
function random(min, max) {
    min = +min || 0
    max = +max || 0
    return Math.random() * (max - min) + min;
}

/**
 * 功能：保留小数，参数为小数，小数保留位数
 */
function cutDecimal(decimal, num) {
    return parseFloat(decimal.toFixed(num));
}

/**
 * 功能：保留两位小数，参数为小数
 */
function cut2Decimal(decimal) {
    return parseFloat(decimal.toFixed(2));
}


/**
 * 功能：获取对象的所有key值列表
 */
function keyList(object) {
    if (object === null || object === undefined) {
        throw new TypeError('Cannot convert undefined or null to object');
    }
    var result = [];
    for (var key in object) {
        object.hasOwnProperty(key) && (result.push(key))
    }
    return result;
}

/**
 * 功能：获取对象的所有value值列表
 */
function valueList(object) {
    if (object === null || object === undefined) {
        throw new TypeError('Cannot convert undefined or null to object');
    }
    var result = [];
    for (var key in object) {
        object.hasOwnProperty(key) && (result.push(object[key]))
    }
    return result;
}